import CookieUtil from '@/utils/CookieUtil'
import {getRegionTree, findGridTree, findUserPermissionTree, getDeptTree, getUserRoles, getAllDictValue, getUserDetail} from '@/http/AppApi'
export const clearLocalStrorage = _ => {
  window.localStorage.clear()
}

export const setDictInfo = data => {
  if (data) {
    window.localStorage.setItem('dict_info', JSON.stringify(data))
  }
}

export const getDictInfo = _ => {
  let dictInfo = window.localStorage.getItem('dict_info')
  if (dictInfo) {
    return JSON.parse(dictInfo)
  }
}

export const getToken = _ => {
  let token = CookieUtil.get('token')
  if (!token) {
    token = window.localStorage.getItem('user_token')
  }
  return token
}

export const setToken = token => {
  window.localStorage.setItem('user_token', token)
}

export const getUserRegionTree = _ => {
  let regionTree = window.localStorage.getItem('user_region_tree')
  if (regionTree) {
    return JSON.parse(regionTree)
  } else {
    getRegionTree().then(resp => {
      if (resp.code && resp.code === '200') {
        let data = resp.data
        if (data && data instanceof Array) {
          window.localStorage.setItem('user_region_tree', JSON.stringify(data))
          return data
        }
      }
    })
  }
}

export const setUserRegionTree = data => {
  if (data && data instanceof Array) {
    window.localStorage.setItem('user_region_tree', JSON.stringify(data))
  }
}

export const getUserInfo = _ => {
  let userInfo = window.sessionStorage.getItem('user_info')
  if (userInfo) {
    return JSON.parse(userInfo)
  } else {
    getUserDetail().then(resp => {
      if (resp.code && resp.code === '200') {
        return resp.data
      }
    })
  }
}

export const setUserInfo = data => {
  if (data) {
    window.sessionStorage.setItem('user_info', JSON.stringify(data))
  }
}

export const getAllDictValues = _ => {
  let dictValue = window.localStorage.getItem('all_dict_value')
  if (dictValue) {
    return JSON.parse(dictValue)
  } else {
    getAllDictValue().then(resp => {
      if (resp.code && resp.code === '200') {
        let data = resp.data
        if (data && data instanceof Array) {
          window.localStorage.setItem('all_dict_value', JSON.stringify(data))
          return data
        }
      }
    })
  }
}

export const getUserGridTree = _ => {
  let gridTree = window.localStorage.getItem('user_grid_tree')
  if (gridTree) {
    return JSON.parse(gridTree)
  } else {
    findGridTree().then(resp => {
      if (resp.code && resp.code === '200') {
        let data = resp.data
        if (data && data instanceof Array) {
          window.localStorage.setItem('user_grid_tree', JSON.stringify(data))
          return data
        }
      }
    })
    return JSON.parse(window.localStorage.getItem('user_grid_tree'))
  }
}

export const setUserGridTree = data => {
  if (data && data instanceof Array) {
    window.localStorage.setItem('user_grid_tree', JSON.stringify(data))
  }
}

export const getUserPermissionTree = _ => {
  let userPermission = window.localStorage.getItem('user_permission_tree')
  if (userPermission) {
    return JSON.parse(userPermission)
  } else {
    findUserPermissionTree().then(resp => {
      if (resp.code && resp.code === '200') {
        let data = resp.data
        if (data && data instanceof Array) {
          window.localStorage.setItem('user_permission_tree', JSON.stringify(data))
          return data
        }
      }
    })
  }
}

export const setUserPermissionTree = data => {
  if (data && data instanceof Array) {
    window.localStorage.setItem('user_permission_tree', JSON.stringify(data))
  }
}

export const getUserDeptTree = _ => {
  let deptTree = window.localStorage.getItem('user_dept_tree')
  if (deptTree) {
    return JSON.parse(deptTree)
  } else {
    getDeptTree().then(resp => {
      if (resp.code && resp.code === '200') {
        let data = resp.data
        if (data && data instanceof Array) {
          window.localStorage.setItem('user_dept_tree', JSON.stringify(data))
          return data
        }
      }
    })
  }
}

export const setUserDeptTree = data => {
  if (data && data instanceof Array) {
    window.localStorage.setItem('user_dept_tree', JSON.stringify(data))
  }
}

export const setUserRoleList = data => {
  if (data && data instanceof Array) {
    window.localStorage.setItem('user_roles', JSON.stringify(data))
  }
}

export const getUserRoleList = _ => {
  let list = window.localStorage.getItem('user_roles')
  if (list) {
    return JSON.parse(list)
  } else {
    getUserRoles().then(resp => {
      if (resp.code && resp.code === '200') {
        let data = resp.data
        if (data && data instanceof Array) {
          window.localStorage.setItem('user_roles', JSON.stringify(data))
          return data
        }
      }
    })
  }
}
